package vklib.emulator
{
    import flash.external.*;
    import flash.net.*;

    public class VkExternal extends Object
    {
        private const CB_SETTINGS:String = "extShowSettingsBox";
        private var _currentTitle:String = "";
        private var _currentNav:String = "";
        private const CB_PAYMENT:String = "extShowPaymentBox";
        private const CB_RESIZE:String = "extResizeWindow";
        private const CB_LOCATION:String = "extSetLocation";
        private const CB_TITLE:String = "extSetTitle";
        private const CB_INSTALL:String = "extShowInstallBox";
        private const CB_WALL_POST:String = "extSaveWallPost";
        private const CB_INVITE_FRIENDS:String = "extShowInviteBox";
        private const CB_DEBUG:String = "extDebug";
        private const CB_PROFILE_PIC:String = "extShowProfilePhotoBox";
        private const CB_SCROLL:String = "extScrollWindow";
        private const CB_NAVIGATION:String = "extSetNavigation";
        private var _currentLoc:String = "";
        private static var _instance:VkExternal;
        private static var _allowInst:Boolean;

        public function VkExternal()
        {
            if (!_allowInst)
            {
                throw new Error("Error: Instantiation failed.");
            }
            return;
        }// end function

        public function debug(param1:*) : void
        {
            var lines:Array;
            var i:uint;
            var value:* = param1;
            if (!value.toString)
            {
                return;
            }
            var config:* = VkConfig.getConfig();
            var message:* = value.toString();
            try
            {
                if (config.debug)
                {
                    lines = message.split("\n");
                    i;
                    while (i < lines.length)
                    {
                        
                        lines[i] = "SWF DEBUG: " + lines[i];
                        i = (i + 1);
                    }
                    message = this.EscapeMessage(lines.join("\n"));
                    this.call(this.CB_DEBUG, message);
                }
                else
                {
                    trace(message);
                }
            }
            catch (ex:Error)
            {
                trace(ex);
            }
            return;
        }// end function

        public function showPaymentBox(param1:uint = 0) : void
        {
            this.call(this.CB_PAYMENT, param1);
            return;
        }// end function

        public function resizeWindow(param1:uint, param2:uint, param3:Boolean = false) : void
        {
            var _loc_4:* = VkConfig.getConfig();
            VkConfig.getConfig().width = param1;
            _loc_4.height = param2;
            this.call(this.CB_RESIZE, param1, param2, param3);
            return;
        }// end function

        private function EscapeObject(param1:Object) : Object
        {
            var _loc_2:String = null;
            for (_loc_2 in param1)
            {
                
                param1[_loc_2] = this.EscapeMessage(param1[_loc_2]);
            }
            return param1;
        }// end function

        private function EscapeMessage(param1:*):*
        {
            if (param1 is String)
            {
                param1 = this.EscapeString(param1);
            }
            else if (param1 is Array)
            {
                param1 = this.EscapeArray(param1);
            }
            else if (param1 is Object)
            {
                param1 = this.EscapeObject(param1);
            }
            return param1;
        }// end function

        public function navigateToURL(param1:URLRequest, param2:String = "_self") : void
        {
            var _loc_3:* = VkConfig.getConfig().domain;
            var _loc_4:* = param1.url.match(/^(http:\/\/){0,1}([a-z0-9_]+\.){0,1}(vkontakte\.ru|vk\.com)(\/){0,1}(.*)/i);
            if (param1.url.match(/^(http:\/\/){0,1}([a-z0-9_]+\.){0,1}(vkontakte\.ru|vk\.com)(\/){0,1}(.*)/i))
            {
                param1.url = "http://" + (_loc_4[2] ? (_loc_4[2]) : ("")) + _loc_3 + "/" + _loc_4[5];
            }
            else
            {
                param1.url = "http://" + _loc_3 + "/" + param1.url;
            }
            navigateToURL(param1, param2);
            return;
        }// end function

        public function set currentLoc(param1:String) : void
        {
            this._currentLoc = param1;
            return;
        }// end function

        public function setLocation(param1:String, param2:Boolean = false) : void
        {
            this._currentLoc = param1;
            this.call(this.CB_LOCATION, param1, param2);
            return;
        }// end function

        public function setNavigation(param1:Array = null, param2:Array = null) : void
        {
            var _loc_3:String = "";
            if (param1 != null)
            {
                _loc_3 = _loc_3 + (param1.join("#") + "#");
            }
            if (param2 != null)
            {
                _loc_3 = _loc_3 + param2.join("#");
            }
            if (_loc_3 != this._currentNav)
            {
                this.call(this.CB_NAVIGATION, param1, param2);
                this._currentNav = _loc_3;
            }
            return;
        }// end function

        private function EscapeString(param1:String) : String
        {
            var _loc_2:* = /\\/g;
            return param1.replace(_loc_2, "\\\\");
        }// end function

        public function showInviteBox() : void
        {
            this.call(this.CB_INVITE_FRIENDS);
            return;
        }// end function

        private function call(... args) : Boolean
        {
            args = null;
            if (ExternalInterface && ExternalInterface.available)
            {
                args = args as Array;
                return ExternalInterface.call.apply(null, args);
            }
            return false;
        }// end function

        public function scrollWindow(param1:int, param2:uint = 0) : void
        {
            this.call(this.CB_SCROLL, param1, param2);
            return;
        }// end function

        public function showInstallBox() : void
        {
            this.call(this.CB_INSTALL);
            return;
        }// end function

        public function showProfilePhotoBox(param1:String) : void
        {
            this.call(this.CB_PROFILE_PIC, param1);
            return;
        }// end function

        public function showSettingsBox(param1:uint = 0) : void
        {
            this.call(this.CB_SETTINGS, param1);
            return;
        }// end function

        public function saveWallPost(param1:String) : void
        {
            this.call(this.CB_WALL_POST, param1);
            return;
        }// end function

        public function getLocation() : String
        {
            return this._currentLoc;
        }// end function

        public function setTitle(param1:String) : void
        {
            this._currentTitle = param1;
            this.call(this.CB_TITLE, param1);
            return;
        }// end function

        private function EscapeArray(param1:Array) : Array
        {
            var _loc_2:* = param1.length;
            var _loc_3:uint = 0;
            while (_loc_3 < _loc_2)
            {
                
                param1[_loc_3] = this.EscapeMessage(param1[_loc_3]);
                _loc_3 = _loc_3 + 1;
            }
            return param1;
        }// end function

        public static function getInstance() : VkExternal
        {
            if (_instance == null)
            {
                _allowInst = true;
                _instance = new VkExternal;
                _allowInst = false;
            }
            return _instance;
        }// end function

    }
}
